package com.itheima.service.impl;

import com.itheima.dto.OrderSettingDto;
import com.itheima.mapper.OrdersettingMapper;

import com.itheima.service.OrdersettingService;
import com.itheima.vto.OrderSettingPre;
import com.itheima.vto.OrderSettingVto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrdersettingServiceImpl implements OrdersettingService {
    @Autowired
    private OrdersettingMapper ordersettingMapper;
    /**
     * 根据日期修改可预约人数
     * @param orderSettingDto
     * @return
     */
    public boolean editNumberByOrderDate(OrderSettingDto orderSettingDto) throws ParseException {
        return ordersettingMapper.editNumberByOrderDate(orderSettingDto);
    }

    /**
     * 根据年月查询对应的预约设置信息
     * @param year
     * @param monthNumber
     * @return
     */
    public List<OrderSettingVto> getOrderSettingByMonth(int year, int monthNumber) {
        //获取数据库类型的数组
        List<OrderSettingPre> list = ordersettingMapper.getOrderSettingByMonth( year, monthNumber);
        //转换为前端需要的类型
        List<OrderSettingVto> list1 = new ArrayList<>();
        list.forEach(s->{
            String[] parts = s.getOrderDate().toString().split("-");
            OrderSettingVto orderSettingVto = new OrderSettingVto();
            orderSettingVto.setDate(parts[2]);
            orderSettingVto.setNumber(s.getNumber());
            orderSettingVto.setReservations(s.getReservations());
            list1.add(orderSettingVto);
        });
        return list1;
    }

    /**
     * 增加预约设置数据
     * @param list
     */
    public void addData(List<OrderSettingDto> list) {
        for(OrderSettingDto orderSettingDto:list){
            ordersettingMapper.addData(orderSettingDto);
        }
    }


}
